'''
Created on Dec 28, 2011

@author: onetera
'''
from PyQt4 import QtCore , QtGui
from lib.papyDB import papyDB , tableInfo
from lib.papyDBTable import *
import pickle

class toolDataHandler:
    def __init__(self , toolName  ):
        self.userID = papyDB.userID()
        self.toolName = toolName
        
    def pickelLoads(self ):
        pickData = self.getPickleData()        
        if   pickData  == None or str( pickData ) == '':
            return  appInfo()
        else : 
            return pickle.loads( str( pickData ) )
           
    def setPickelData(self , data ):              
        pickleData = pickle.dumps( data )
        result = TOOLS_INFO.search( 'data' , userID = self.userID , toolName = self.toolName )      
        if result != []:                                    
            TOOLS_INFO.update( 'data' , str( pickleData ) , userID = self.userID , toolName = self.toolName )
        else :             
            TOOLS_INFO.register( self.userID, self.toolName , pickleData )
        
    def getPickleData(self):
        theData = TOOLS_INFO.search( 'data' , userID = self.userID , toolName = self.toolName )        
        if theData == [] : return        
        return theData 

   
class appInfo:
    def __init__(self ):        
        self.pos = [0,0]
        self.size = [0,0]        
        self.appData = {}   
#        self.app = app 

    def geometry(self):
        if self.pos[0] < 0 :
            self.pos[0] = 100
        if  self.pos[1] < 0 :
            self.pos[1] = 100
        return self.pos + self.size
                
    def setCustomInfo(self , **data ):        
        if data != None:
            for x in data:
                self.appData[ x ] = data[ x ]  
    def __getitem__(self , key):
        if key in self.appData.keys():
            return self.appData[key]
        else : 
            self.appData[key] = 0
            return 0
    

class Asset:
    def __init__(self, assetID , assetName  , assetStatus  , assetType  , assetRigging , assetTexture , assetDesc ):
        self.assetID = assetID
        self.assetName = QtCore.QString( assetName )        
        self.assetStatus = QtCore.QString( assetStatus )
        self.assetType = QtCore.QString( assetType )
        self.assetRigging = QtCore.QString( assetRigging )
        self.assetTexture = QtCore.QString( assetTexture )
        if assetDesc == None :
            assetDesc = ''
        self.assetDesc = QtCore.QString( assetDesc )
        self.assetVersion = QtCore.QString(  )
        self.assetVersionList = []
        

        
class AssetDate:
    def __init__ (self, assetID, assetName, assetStatus, assetStartDate, assetEndDate, assetFinalDate, assetDesc):
        self.assetID = assetID
        self.assetName = QtCore.QString( assetName )
        self.assetStatus = QtCore.QString( assetStatus )  
        self.assetStartDate = QtCore.QString( assetStartDate )  
        self.assetEndDate = QtCore.QString( assetEndDate )  
        self.assetFinalDate = QtCore.QString( assetFinalDate )  
        if assetDesc == None :
            assetDesc = ''
        self.assetDesc = QtCore.QString( assetDesc )
        
class treeItem(object):
    def __init__(self , data  , parent = None  ):
        self.parentItem = parent
        self.itemData = data
        self.childItems = []
    
    def child(self, row):        
        return self.childItems[row]
    
    def childCount(self):
        return len( self.childItems )    
    
    def childNumber(self):
        if self.parentItem != None:
            return self.parentItem.childItems.index( self )
        return 0
    
    def columnCount(self):
        return 1
    
    def data( self ):
        return self.itemData
    
    def parent(self):
        return self.parentItem

    def insertChildren(self, position, count):
        if position < 0 or position > len(self.childItems):
            return False
        for row in range(count):
            data = None 
            item = treeItem(data, self)
            self.childItems.insert(position, item)
        return True
    
              
        
            
    
if __name__ == '__main__':
    appInfo = appInfo()
    print appInfo[4]
#    aa = pickle.dumps( AssetDate )
#    TOOLS_INFO.update( 'data' , aa , userID = 1 , toolName = 'shotAssembler' )    
